#! /usr/bin/bash

SCRIPT=$(basename $0)
function usage(){
    echo -e "\nUSAGE: $SCRIPT <dir> \n"
    exit 1
}

if [ $# -lt 1 ] ; then
   usage
fi

INDEX_LIST_FILE=/tmp/idx/idxlist.txt
INDEX_CK_FILE=/tmp/idx/idxck.sh
INDEX_CK_RSLT=/tmp/idx/idxckrslt.txt
foreachd(){
	for file in $1/*
		do
			if [ -d $file ]
			then
				foreachd $file
			elif [ -f $file ]
			then
				echo $file | egrep ".*/[0-9]+/segments.gen" >> $INDEX_LIST_FILE
			fi
		done
}

rm -rf $INDEX_LIST_FILE

foreachd "$1"

cat $INDEX_LIST_FILE | sed s/segments.gen// | awk -f idx-cmd.awk > $INDEX_CK_FILE

bash $INDEX_CK_FILE > $INDEX_CK_RSLT


IFS="
"
indexName=""
result="1"

for line in `cat $INDEX_CK_RSLT`
do
	name=`echo $line | egrep "Opening index @ ." | sed s/'Opening index @ '//`
	if [ "$name" != "" ] ;then
            if [ "$result" != "0" ] ;then
	      echo $indexName
	    fi
	    indexName=$name
	    result="1"
	fi
	
	if [ "$line" = "No problems were detected with this index." ] ; then
       	    result="0"
	fi
done

if [ $result != "0" ] ;then
    echo $indexName
fi
